/*
 * TicTacToeBoardComponent.fx
 *
 * Created on Apr 27, 2009, 2:37:29 AM
 */

package com.dg.gaming.tictactoe.client.fx;

import com.dg.gaming.client.fx.gui.DGGameController;
import com.dg.gaming.tictactoe.client.fx.TickTacToeComponent;
import com.dg.gaming.tictactoe.common.TicTacToeModel;
import java.awt.Dimension;
import java.awt.Point;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 * @author dito
 */

public class TicTacToeBoardComponent extends CustomNode{
    // Declare constants which define what the board looks like
    var NUM_OF_ROWS_COLS = 3;
    var  CELL_SIZE = 65;
    var  CELL_SPACING = 1;

    // Link to the model
    public  var model:TicTacToeModel ;

       /** Size of cell in pixels. */
    protected var cellSize:Integer;

    /** Spacing between cells in pixels. */
    protected var cellSpacing:Integer;

        /** Width of the border in pixels. */
    protected var borderWidth:Integer;

    /** If this variable is true the board will be flipped. */
    protected var isReversed:Boolean;


    /** Number of rows in the board. */
    protected var numOfRows:Integer;

    /** Number of columns in the board. */
    protected var numOfCols:Integer;

        /** Colour of cell one. */
    protected var cellColour1:Color;

    /** Colour of cell two. */
    protected var cellColour2:Color;

    /** Colour of spacing colour. */
    protected var spacingColour:Color;

    /** Colour of the mouse border. */
    protected var mouseBorderColour:Color;

        /** Width of the mouse border. */
    protected var mouseBorderWidth:Integer;

        /** Boolean to indicate alternate style background. i.e. chess style board. */
    protected var isAlternateBackground:Boolean;

        /** If true letters/numbers are drawn on the left and bottom of the board. */
    protected var isDrawLetters:Boolean;


    

    public override  function create():Node {
        initBoard(NUM_OF_ROWS_COLS, NUM_OF_ROWS_COLS, CELL_SIZE, CELL_SPACING,
              0, 0, false, false, false);
        setColours(Color.WHITE, Color.WHITE, Color.BLACK, Color.WHITE);
       
        return Group {
                content: [
                for(x1 in [0..numOfCols-1]){
                    for(y1 in [0..numOfCols-1]){
                        var screenX = x1 * (this.cellSize + this.cellSpacing) +
                                  this.cellSpacing +
                                  this.borderWidth;
                        var screenY = y1 * (this.cellSize + this.cellSpacing) +
                                  this.cellSpacing +
                                  this.borderWidth;
                        TickTacToeComponent{
                            x:screenX
                            y:screenY
                            X1:x1
                            Y1:y1
                            stroke:Color.BLACK
                            model:model
                            height:this.cellSize
                            width:this.cellSize
                            
                        }
                    }
                }


                   
                ]
       }
      }
      
public function getScreenCoords(boardX:Integer, boardY:Integer):Point {
        var totalCellSize = this.cellSize + this.cellSpacing;

        // compute screen X and Y
        var screenX = (boardX * totalCellSize) + this.borderWidth +
                      this.cellSpacing;
        var screenY = (boardY * totalCellSize) + this.borderWidth +
                      this.cellSpacing;

        if (this.isReversed) {
            screenX = ((this.numOfCols - 1) * totalCellSize) -
                      (boardX * totalCellSize) + this.borderWidth +
                      this.cellSpacing;
            screenY = ((this.numOfRows - 1) * totalCellSize) -
                      (boardY * totalCellSize) + this.borderWidth +
                      this.cellSpacing;
        }

        return new Point(screenX, screenY);
    }
    



    public function getBoardCoords(screenX:Integer, screenY:Integer): Point{
        var totalCellWidth = this.cellSize + this.cellSpacing;

        // compute new board X and Y
        var boardX = ((screenX - this.borderWidth) / totalCellWidth);
        var boardY = ((screenY - this.borderWidth) / totalCellWidth);

        // note if the board is reversed then we have to allow for this also
        if (this.isReversed) {
            boardX = (this.numOfCols - 1) - boardX;
            boardY = (this.numOfRows - 1) - boardY;
        }

        return new Point(boardX, boardY);
    }

    public function setController(controller:DGGameController ) {
        
        onKeyPressed=controller.keyPressed;
        onKeyReleased=controller.keyReleased;
        onKeyTyped = controller.keyTyped;

        onMouseClicked = controller.mouseClicked;
        onMouseDragged = controller.mouseDragged;
        onMouseEntered = controller.mouseEntered;
        onMouseExited = controller.mouseExited;
        onMouseMoved = controller.mouseMoved;
        onMousePressed = controller.mousePressed;
        onMouseReleased = controller.mouseReleased;
        

    }

    public function setColours(cellColour1:Color , cellColour2:Color ,
                                 spacingColour:Color , mouseBorderColour:Color ) {
        this.cellColour1 = cellColour1;
        this.cellColour2 = cellColour2;
        this.spacingColour = spacingColour;
        this.mouseBorderColour = mouseBorderColour;
    }
 var size:Dimension;
    public function initBoard(numOfRows:Integer,
            numOfCols:Integer,
            cellSize:Integer,
            cellSpacing:Integer,
            borderWidth:Integer,
            mouseBorderWidth:Integer,
            isReversed:Boolean,
            alternateBackground:Boolean,
            drawLetters:Boolean){
                this.numOfRows = numOfRows;
        this.numOfCols = numOfCols;
        this.cellSize = cellSize;
        this.cellSpacing = cellSpacing;
        this.borderWidth = borderWidth;
        this.mouseBorderWidth = mouseBorderWidth;
        this.isReversed = isReversed;
        this.isAlternateBackground = alternateBackground;
        this.isDrawLetters = drawLetters;
        var boardWidth =
                (numOfCols * (cellSize + cellSpacing)) +
                (borderWidth * 2) +
                cellSpacing;
        var boardHeight =
                (numOfRows * (cellSize + cellSpacing)) +
                (borderWidth * 2) +
                cellSpacing;
               size= new Dimension(boardWidth, boardHeight);
        /*setColours(
                new Color(255, 244, 200),
                new Color(200, 170, 100),
                new Color(64, 64, 64),
                new Color(64, 255, 0)
                );*/
    }

}
